package chain_config

import (
	"chain_bridge_service/configs"
	"context"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"go.mongodb.org/mongo-driver/mongo"
)

func NewModel() *ChainConfig {
	return new(ChainConfig)
}

func (c *ChainConfig) List(client *mongo.Client) ([]ChainConfig, error) {
	var list []ChainConfig
	cfg := configs.Get().Mongodb
	keyClient := client.Database(cfg.Config.Name).Collection("chain_config")
	//find, err := keyClient.Find(context.TODO(), bson.D{{"type", "DAPP"}})
	find, err := keyClient.Find(context.TODO(), bson.D{})
	if err != nil {
		return nil, err
	}
	err = find.All(context.TODO(), &list)
	return list, nil
}

func (*ChainConfig) GetById(db *mongo.Client, chainConfigId string) (ChainConfig, error) {
	var chainConfig ChainConfig
	cfg := configs.Get().Mongodb
	keyClient := db.Database(cfg.Config.Name).Collection("chain_config")
	objId, _ := primitive.ObjectIDFromHex(chainConfigId)
	err := keyClient.FindOne(context.TODO(), bson.D{{"_id", objId}}).Decode(&chainConfig)
	return chainConfig, err
}
